import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import sklearn
from sklearn.neural_network import MLPClassifier
from sklearn.neural_network import MLPRegressor

# Import necessary modules
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from math import sqrt
from sklearn.metrics import r2_score

from sklearn.metrics import classification_report, confusion_matrix

# 加载数据
df = pd.read_csv("diabetes.csv")
print(df.shape)
df.describe().transpose()
print(df.head(10))
# 预处理归一化
target_column = ["Outcome"]
predictors = list(set(list(df.columns)) - set(target_column))
df[predictors] = df[predictors] / df[predictors].max()
df.describe().transpose()
print(df.head(10))
# 划分训练集和测试集
X = df[predictors].values
y = df[target_column].values
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.30, random_state=40
)
# 训练模型
mlp = MLPClassifier(
    hidden_layer_sizes=(8, 8, 8), activation="relu", solver="adam", max_iter=500
)
mlp.fit(X_train, y_train)
# 预测
predict_train = mlp.predict(X_train)
predict_test = mlp.predict(X_test)
# 评估训练集
print(confusion_matrix(y_train, predict_train))
print(classification_report(y_train, predict_train))

# 评估测试集
print(confusion_matrix(y_test, predict_test))
print(classification_report(y_test, predict_test))
